#include "LightUnit.h"
#include "Common.h"
#include "Eitoboru.h"

LightUnit::LightUnit( scene::ISceneNode *node, scene::ILightSceneNode *light )
{
	m_Node = node;
	m_Light = light;
	m_Rotation = new core::vector3df( 0, 0, 0 );
	m_Velocity = new core::vector3df( 0, 0, 0 );
	m_curAngleReached = new bool;
	m_curMaxAngle = new float;
	m_maxAngle = new float;	

	*m_curAngleReached = true;
	*m_curMaxAngle = 45.0f;
	*m_maxAngle = 45.0f;
}

LightUnit::~LightUnit( void )
{
	if(m_Node)
		delete m_Node;
	if(m_Rotation)
		delete m_Rotation;
	if(m_curAngleReached)
		delete m_curAngleReached;
	if(m_curMaxAngle)
		delete m_curMaxAngle;
	if(m_maxAngle)
		delete m_maxAngle;
}

void LightUnit::CalculateLightPosition( core::vector3df *position )
{
	core::vector3df tempVar(position->X, position->Y+2, position->Z);
	core::vector3df lightPos;
	float rotation = (m_Rotation->Z-90) * core::DEGTORAD;
	lightPos.X = cos(rotation)*tempVar.Y - sin(rotation)*tempVar.Z;
	lightPos.Y = sin(rotation)*tempVar.Y + cos(rotation)*tempVar.Z;
	lightPos.X -= position->X;
	lightPos.Y += position->Y;
	lightPos.Z -= position->Z;
	m_Light->setPosition( lightPos );
}

void LightUnit::Update( void )
{
	//Retrieve position
	core::vector3df position = m_Node->getPosition();
	
	//When hit do the following
	if(!m_Velocity->equals(core::vector3df(0, 0, 0)))
	{
		*m_Rotation = *m_Rotation + *m_Velocity;
		if(abs(m_Rotation->Z) < *m_curMaxAngle && *m_curAngleReached == true)
		{}
		else if(*m_curMaxAngle <= 0)
		{
			if( abs(m_Rotation->X) < 1 && abs(m_Rotation->Y) < 1 && abs(m_Rotation->Z) < 1)
			{
				*m_curMaxAngle = *m_maxAngle;
				m_Velocity->set( 0, 0, 0 );
			}
		}
		else if(*m_curAngleReached == false)
		{
			if(abs(m_Rotation->Z) < *m_curMaxAngle)
			{
				*m_curAngleReached = true;
			}
		}
		else
		{
			*m_Velocity = m_Velocity->invert();
			*m_Velocity *= 0.9;
			*m_curMaxAngle -= 5;

			*m_curAngleReached = false;
		}
	}

	//Calculate position of the node
	m_Node->setRotation( *m_Rotation );
	
	//Calculate position of the light
	CalculateLightPosition( &position );
}

void LightUnit::SmashLight(core::vector3df force)
{
	m_Velocity = new core::vector3df( 0, 0, 1 );
}

//Private functions
core::vector3df *LightUnit::MatrixRotation( core::vector3df *translate, core::vector3df *rotation )
{
	float x, y, z;
	
	/* Rotation around Z */ 
	x = translate->X*cos(rotation->Z) - translate->Y*sin(rotation->Z);
	y = translate->X*sin(rotation->Z) + translate->Y*cos(rotation->Z);
	z = translate->X*cos(rotation->Z) - translate->Y*sin(rotation->Z);

	/* Rotation around Y */ /*
	x = translate->X*cos(rotation->Y) - translate->Z*sin(rotation->Y);
	y = 0;
	z = -translate->X*sin(rotation->Y) + translate->Z*cos(rotation->Y);*/

	/* Rotation around X */ /*
	x = 0;
	y = translate->Y*cos(rotation->X) - translate->Z*sin(rotation->X);
	z = translate->Y*sin(rotation->X) + translate->Z*cos(rotation->X);*/

	translate->X = x;
	translate->Y = y;
	translate->Z = z;

	return translate;
}